package com.itcast.spring6.ioc.bean;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;

/**
 * 外部配置化依赖来源
 * @author zhangtian105
 * @date 2023/5/4
 * @apiNote
 */
@Slf4j
@Configuration
@PropertySource(value = "META-INF/default.properties", encoding = "UTF-8")
public class ExternalConfigurationDependenceTest {
    @Value("${user.id:-1}")
    private Long id ;
    @Value("${user.name:水梦星光}")
    private String name ;
    @Value("${user.resource:classpath://default.properties}")
    private Resource resource ;

    public static void main(String[] args) {
        // 创建BeanFactory容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext() ;
        // 注册ExternalConfigurationDependenceTest Class（配置类）
        applicationContext.register(ExternalConfigurationDependenceTest.class);

        // 启动spring应用上下文
        applicationContext.refresh();

        // 依赖查找ExternalConfigurationDependenceTest Bean
        ExternalConfigurationDependenceTest test = applicationContext.getBean(ExternalConfigurationDependenceTest.class) ;
        log.info("test.id = {}", test.id);
        log.info("test.name = {}", test.name);
        log.info("test.resource = {}", test.resource);

        // 关闭spring应用上下文
        applicationContext.close();
    }
}
